/* Copyright (c) 2008 Google Inc.
*  Copyright (c) 2009 Pierre Henri Kuate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace org.opensocial.data
{
    /// <summary> An object representing OpenSocial App Data; extends OpenSocialObject class
    /// by adding a convenience method for getting the data value associated with a
    /// particular key for a particular user.
    /// </summary>
    /// <author>  Jason Cooper
    /// </author>
    public class AppData : OpenSocialObject
    {
        /// <summary> Retrieves the String-based App Data value associated with the passed key
        /// for the user with the passed ID.
        /// </summary>
        /// <param name="id">User ID corresponding to the user whose App Data is desired
        /// </param>
        /// <param name="key">App Data key corresponding to the desired value
        /// </param>
        /// <throws>  OpenSocialException </throws>
        public string getStringForUser(string id, string key)
        {
            OpenSocialField f1;
            OpenSocialField f2;
            OpenSocialObject o;
            string value;

            try
            {
                f1 = getField(id);
                o = f1.Value;

                f2 = o.getField(key);
                value = f2.StringValue;

                return value;
            }
            catch
            {
                return null;
            }
        }

        public string[] getFieldNamesForUser(string id)
        {

            OpenSocialField f1;
            OpenSocialObject o;

            try
            {
                f1 = getField(id);
                o = f1.Value;

                return o.fieldNames();
            }
            catch
            {
                return null;
            }
        }
    }
}